package com.xmall.search.handler.msg;

import cn.hutool.json.JSONUtil;
import com.rabbitmq.client.Channel;
import com.xmall.config.AmqpCommonConfig;
import com.xmall.msg.queue.SearchModule;
import com.xmall.msg.entity.es.ProductUpdateMsg;
import com.xmall.search.service.IProductDeleteRetryLogService;
import com.xmall.search.service.other.IProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * sku更新
 */
@Slf4j
@Component
@RabbitListener(queues = SearchModule.ProductDelete.PRODUCT_DELETE_QUEUE, containerFactory = AmqpCommonConfig.RABBIT_LISTENER_CONTAINER_FACTORY_NAME)
public class ProductDeleteHandler {

    @Resource
    private IProductService productService;

    @Resource
    private IProductDeleteRetryLogService productDeleteRetryLogService;

    @RabbitHandler
    public void msg(Channel channel, Message message, ProductUpdateMsg msg) throws IOException {
        log.info("rabbitmq handler: search service: ProductDeleteHandler: {}", JSONUtil.toJsonStr(msg));
        productDeleteRetryLogService.saveLog(msg);
        try {
            productService.delete(msg);
        }catch (Exception e) {
            log.error("rabbitmq handler: search service: ProductDeleteHandler error: {}", e.getMessage());
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
